package com.luhui.source;

/**
 * 带前缀的自定义属性源父类
 * @author:Luhui
 * @qq:729913162
 * @Date:2018/11/3 0003
 * @Time:17:44
 */
public abstract class PrefixCustomPropertySource<T> extends CustomPropertySource<T> {


    public PrefixCustomPropertySource(String name, T source) {
        super(name, source);
    }

    public PrefixCustomPropertySource(String name) {
        super(name);
    }

    /**
     * 键的属性前缀
     * @return
     */
    protected abstract String getPropertyPrefix();

    @Override
    public final Object getProperty(String name) {
        String prefix = getPropertyPrefix();
        if(!prefix.endsWith(".")){
            prefix +=".";
        }
        if(!name.startsWith(prefix)){
            return null;
        }else{
            String key = name.substring(prefix.length());
            return getCustomProperty(key);
        }
    }

    /**
     * 根据自定义的逻辑，根据键名取出键值。
     * @param name
     * @return
     */
    protected abstract Object getCustomProperty(String name);
}
